package com.example.maze;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.View;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.EditText;

import receivers.LoginRepReceiver;
import services.LoginService;
//import tw.edu.tku.im.maze.*;


public class MainActivity extends Activity{
   
   //[ ---------- Life cycle ----------
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      mProgressDialog= new MyProgressDialog(this);
      //mProgressDialog.setIndeterminate(true);
      LoginRepReceiver mLoginRepReceiver= new LoginRepReceiver(this);
      LocalBroadcastManager.getInstance(this).registerReceiver(mLoginRepReceiver,
                                                               new IntentFilter(LoginService.BROADCAST_ACTION));
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.activity_login, menu);
      return true;
   }
   //] ---------- Life cycle ----------
   
   
   // -------------------------------------------------- 
   MyProgressDialog mProgressDialog;
   public void showMyDialog(String title, String msg) {
      if(!mProgressDialog.hasShowed()) {
         mProgressDialog.show("","帳號驗證中, 請稍候");
      } else {
         mProgressDialog.dismiss();
      }
   }   
   public void dismissMyDialog() {
      if(mProgressDialog.hasShowed()) {
         mProgressDialog.dismiss();
      }
   }
   // --------------------------------------------------
   //[ 登入按鈕事件處理
   public void button_loginOnClicked(View view){      
      EditText editText_userId = (EditText)this.findViewById(R.id.editText_userId);
      EditText editText_userPwd= (EditText)this.findViewById(R.id.editText_userPwd);
      this.showMyDialog("", "帳號驗證中, 請稍候");
            
      //[ 開 service thread背景處理
      Intent mServiceIntent= new Intent(this, LoginService.class);
      mServiceIntent.putExtra("id", editText_userId.getText().toString());
      mServiceIntent.putExtra("pwd", editText_userPwd.getText().toString());       
      this.startService(mServiceIntent);
   }
   // --------------------------------------------------
   //[ 註冊按鈕事件處理
   public void button_registerOnClicked(View view) {
      Intent intent= new Intent(this, RegisterActivity.class);
      this.startActivity(intent);
   }
   // --------------------------------------------------      
   //[ 印出錯誤訊息
   public void showMsgDialog(int externalStrResourseId) {
      //[ 系統訊息視窗
      try {
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setPositiveButton(R.string.dialog_ok_ch, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
               // User clicked OK button
            }
         });         
         //builder.setMessage(externalStrResourseId);
         builder.setMessage(externalStrResourseId);
         AlertDialog dialog = builder.create();
         if(dialog==null) MyLog.d("dialog==null", getClass().getName(), 45);
         dialog.show();
         MyLog.d("errorHandler6", getClass().getName(), 50); 
      } catch(Exception e) {
         MyLog.d("errorHandler7", e.getMessage(), 50); 
      }      
   }
}
